﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foothill.Model;
using System.Linq;

namespace Foothill.Service.AdService
{
    public static class AdListFilterExtension
    {
        internal static IEnumerable<ForSaleAd> FilterMakeAndModel(this IEnumerable<ForSaleAd> adList, ForSaleAdAdvancedSearchParameter searchParam)
        {
            if (searchParam.IncludeAllMakes)
            {
                return adList;
            }

            IList<int> modelIdList;
            IList<int> makeIdList;

            searchParam.GetSelectedMakeAndModelIdList(out makeIdList, out modelIdList);

            return adList.Where(c => makeIdList.Contains(c.MakeId) || modelIdList.Contains(c.ModelId));
        }

        internal static IEnumerable<ForSaleAd> FilterVehicleCondition(this IEnumerable<ForSaleAd> adList, ForSaleAdAdvancedSearchParameter searchParam)
        {
            if (searchParam.ConditionId != (int)VehicleCondition.Both)
            {
                return adList.Where(c => c.VehicleConditionId == searchParam.ConditionId);
            }

            return adList;
        }

        internal static IEnumerable<ForSaleAd> FilterListingType(this IEnumerable<ForSaleAd> adList, ForSaleAdAdvancedSearchParameter searchParam)
        {
            if (searchParam.ListingTypeId != (int)ListingTypes.Both)
            {
                return adList.Where(c => c.ListingTypeId == searchParam.ListingTypeId);
            }

            return adList;
        }

        internal static IEnumerable<ForSaleAd> FilterTransmissionType(this IEnumerable<ForSaleAd> adList, ForSaleAdAdvancedSearchParameter searchParam)
        {
            if (searchParam.TransmissionTypeId != (int)TransmissionType.Both)
            {
                return adList.Where(c => c.TransmissionTypeId == searchParam.TransmissionTypeId);
            }

            return adList;
        }

        internal static IEnumerable<ForSaleAd> FilterCities(this IEnumerable<ForSaleAd> adList, ForSaleAdAdvancedSearchParameter searchParam)
        {
            if (!searchParam.AllLocationSelected)
            {
                return adList.Where(c => searchParam.LocationIdList.Contains(c.CityId));
            }

            return adList;
        }

        internal static IEnumerable<ForSaleAd> FilterMileage(this IEnumerable<ForSaleAd> adList, ForSaleAdAdvancedSearchParameter searchParam)
        {
            if (searchParam.SelectedMileageGroupIdList.Count == 0)
            {
                return adList;
            }
            
            IEnumerable<ForSaleAd> filteredResult = new List<ForSaleAd>();
            MileageStatsGroupManager manager = new MileageStatsGroupManager();

            foreach (int groupId in searchParam.SelectedMileageGroupIdList)
            {
                Func<ForSaleAd, bool> groupFunc = manager.GetGroupFunc(groupId);

                filteredResult = filteredResult.Union(adList.Where(groupFunc));
            }
            
            return filteredResult;
        }

        internal static IEnumerable<ForSaleAd> FilterYearAge(this IEnumerable<ForSaleAd> adList, ForSaleAdAdvancedSearchParameter searchParam)
        {
            if (searchParam.SelectedYearAgeGroupIdList.Count == 0)
            {
                return adList;
            }

            IEnumerable<ForSaleAd> filteredResult = new List<ForSaleAd>();
            YearStatsGroupManager manager = new YearStatsGroupManager();

            foreach (int groupId in searchParam.SelectedYearAgeGroupIdList)
            {
                Func<ForSaleAd, bool> groupFunc = manager.GetGroupFunc(groupId);
                filteredResult = filteredResult.Union(adList.Where(groupFunc));
            }

            return filteredResult;
        }
    }
}
